using System;
using System.ComponentModel;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Comteck.Dto.Sys;
using Comteck.Winforms.Controls.Loading;
using Comteck.Winforms.Data;
using Comteck.Winforms.Utils;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using log4net;

namespace Comteck.Winforms {
  /// <summary>
  /// 带皮肤的基类
  /// </summary>
  public partial class FrmSkinBase : XtraForm {

    #region fields & properties

    /// <summary>
    /// 
    /// </summary>
    public static ILog Logger = LogManager.GetLogger(typeof(FrmSkinBase));

    /// <summary>
    /// 关闭界面时是否需要提示框,默认是需要，但是在超时或者升级时，不需要提示
    /// </summary>
    [DefaultValue(true)]
    [Browsable(false)]
    public virtual bool ShowConfirmWhenClose { get; set; } = true;

    /// <summary>
    /// 是否正在这行某个操作
    /// </summary>
    [Browsable(false)]
    public virtual bool IsExecuting { get; set; }

    /// <summary>
    /// 是否正在执行某个操作
    /// </summary>
    public volatile bool IsExecuting_Thread = false;

    /// <summary>
    /// ajax加载框
    /// </summary>
    protected AjaxLoadHelper ajaxLoad = null;

    /// <summary>
    /// 公布对外访问
    /// </summary>
    public AjaxLoadHelper AjaxLoadHelper => ajaxLoad;

    /// <summary>
    /// api地址
    /// </summary>
    [Browsable(false)]
    protected string api = null;

    #endregion

    #region ctor

    /// <summary>
    /// 
    /// </summary>
    public FrmSkinBase() {
      InitializeComponent();
    }

    #endregion

    #region form load

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void FrmSkinBase_Load(object sender, EventArgs e) {
      if (this.DesignMode) {
        return;
      }

      ajaxLoad = new AjaxLoadHelper(this);

      // 设置输入法状态
      this.ImeMode = ImeMode.OnHalf;

      // 无输入法
      this.SetImeMode_Dev(ImeMode.NoControl);
    }

    #endregion

    #region methods

    /// <summary>
    /// 
    /// </summary>
    /// <param name="text">提示信息</param>
    /// <param name="addToLog">是否添加到日志中</param>
    /// <param name="exception">异常信息</param>
    /// <returns></returns>
    protected DialogResult ShowMsg(string text, bool addToLog = false, Exception exception = null) {
      if (exception == null) {
        Logger.Info(text);
      } else {
        Logger.Error(text, exception);
      }

      return DevHelper.ShowMsg(text);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="text">提示信息</param>
    /// <param name="addToLog">是否添加到日志中</param>
    /// <param name="exception">异常信息</param>
    /// <returns></returns>
    protected DialogResult ShowMsg(string text, params object[] args) {
      return DevHelper.ShowMsg(text, args);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="text"></param>
    /// <param name="caption"></param>
    /// <param name="buttons"></param>
    /// <param name="icon"></param>
    /// <returns></returns>
    protected DialogResult ShowMsg(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon) {
      return DevHelper.ShowMsg(text, caption, buttons, icon);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="text"></param>
    /// <param name="caption"></param>
    /// <param name="buttons"></param>
    /// <param name="icon"></param>
    /// <returns></returns>
    protected DialogResult ShowMsg(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton) {
      return DevHelper.ShowMsg(text, caption, buttons, icon, defaultButton);
    }

    protected override CreateParams CreateParams //点击任务栏实现最小化与还原  
      {
      get {
        const int WS_MINIMIZEBOX = 0x00020000;
        CreateParams cp = base.CreateParams;
        cp.Style = cp.Style | WS_MINIMIZEBOX;   // 允许最小化操作    
        return cp;
      }
    }

    /// <summary>
    /// 打开窗口
    /// </summary>
    /// <param name="menu">菜单信息</param>
    /// <param name="IsReOpenWhenExists">是否重新打开窗体(当已存在时)</param>
    public virtual void OpenForm(MenuAuthDto menu, bool IsReOpenWhenExists = false, int? showMode = null, params object[] args) {
    }

    #endregion

  }
}
